<template>
    <div class="add">
        <Form ref="form" :model="form" :rules="ruleValidate" :label-width="80" inline>
                <#list filedNames as f >
                    <FormItem label="${f.chName}" prop="${f.fiName}">
                        <Input v-model="form.${f.fiName}" placeholder="请输入${f.chName}"></Input>
                    </FormItem>
                </#list>
            <FormItem>
                <Button type="primary" @click="handleSubmit('form')">保存</Button>
                <Button type="ghost" @click="handleReset('form')" style="margin-left: 8px">重置</Button>
            </FormItem>
        </Form>
    </div>
</template>
<script>
    import qs from 'querystring'

    export default {
        data() {
            return {
                form: {},
                ruleValidate: {
                     <#list filedNames as f >
                         ${f.fiName}:
                         [
                             {required: true, message: '${f.chName}不能为空', trigger: 'blur'}
                         ],
                     </#list>
        }
        }
        },
        methods: {
            handleSubmit(name) {
                this.$refs[name].validate((valid) => {
                    if (valid) {
                        //保存
                        this.$axios.post("/api/v1/${className?uncap_first}/save", qs.stringify(this.form)).then((resp) => {
                            this.$Message.success(resp.message);
                            this.$router.push('${tableName}');
                        })
                    } else {
                        this.$Message.error('校验失败!');
                    }
                })
            },
            get() {
                var id = this.$route.params.id;
                if (id == -1) {
                    return;
                }
                this.$axios.get("/api/v1/${className?uncap_first}/get", {params: {id: id}}).then((resp) => {
                    this.form = resp.data;
                })
            },
            handleReset(name) {
                this.$refs[name].resetFields();
            }
        }, mounted() {
            this.get();
        }
    }
</script>